<?php
/*
 * MCP 客户端实现（能力注册）
 */
namespace Mcp\Tools;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Schema\ToolAnnotations;
class mcptools {
    #[McpTool(
        name: 'get_latest_music',
        description: '获取最新音乐列表'
    )]
    public function get_latest_music(int $limit, int $page): array
    {
        $this->logInfo("调用getLatestMusic方法 - limit: {$limit}, page: {$page}");
        // 保持原有实现
        $params = array('uniacid' => $_SESSION['uniacid'] ?? 0);
        $pindex = max(1, $page);
        $psize = $limit;
        $total = '';
        $this->logInfo("查询最新音乐列表 - 条件: " . json_encode($params) . ", 分页: {$pindex}/{$psize}");
        $list = pdo_getslice('jy_agentmcp_mp3',$params, array($pindex, $psize),$total,'', '','id desc');
        $result = [
            'success' => true,
            'music_list' => $list,
            'total' => $total
        ];
        $this->logInfo("getLatestMusic方法返回 - 总数: {$total}, 本次返回: " . count($list) . "条");
        return $result;
    }

    /**
     * 搜索音乐（MCP工具）
     */
    #[McpTool(
        name: 'search_music',
        description: '搜索音乐'
    )]
    public function search_music(string $query, int $page): array
    {
        $this->logInfo("调用searchMusic方法 - query: {$query}, page: {$page}");
        if (empty($query)) {
            $this->logError("searchMusic方法参数错误: 搜索关键词不能为空");
            throw new \InvalidArgumentException("搜索关键词不能为空");
        }
        $params = [
            'uniacid' => $_SESSION['uniacid'] ?? 0,
            'title LIKE' => "%{$query}%"
        ];
        
        $pindex = max(1, $page);
        $psize = 10;
        $total = '';
        
        $this->logInfo("搜索音乐 - 关键词: {$query}, 条件: " . json_encode($params) . ", 分页: {$pindex}/{$psize}");
        $list = pdo_getslice('jy_agentmcp_mp3',$params,[$pindex, $psize], $total, '','','id desc');
        $result = [
            'success' => true,
            'count' => count($list),
            'music_list' => $list
        ];
        $this->logInfo("searchMusic方法返回 - 总数: {$total}, 本次返回: " . count($list) . "条");
        return $result;
    }

    /**
     * 播放音乐（MCP工具）
     */
    #[McpTool(
        name: 'play_music',
        description: '播放指定音乐'
    )]
    public function play_music(int $musicId): array
    {
        $this->logInfo("调用playMusic方法 - music_id: {$musicId}");
        $music = pdo_get(
            'jy_agentmcp_mp3',
            ['id' => $musicId, 'uniacid' => $_SESSION['uniacid'] ?? 0]
        );

        if (!$music) {
            $this->logError("playMusic方法错误: 未找到ID为{$musicId}的音乐");
            throw new \RuntimeException("未找到ID为{$musicId}的音乐");
        }

        // 格式化音乐信息
        $result = [
            'success' => true,
            'message' => "正在播放: {$music['title']}",
            'music' => [
                'id' => $music['id'],
                'title' => $music['title'],
                'artist' => $music['artist'] ?? '未知艺术家',
                'url' => tomedia($music['url']),
                'album' => $music['album'] ?? '未知专辑'
            ]
        ];
        $this->logInfo("playMusic方法返回 - 播放音乐: {$music['title']} (ID: {$musicId})");
        return $result;
    }
    /**
     * 日志记录
     */
    public function logInfo(string $message): void
    {
        $this->log("[INFO] {$message}");
    }

    private function logWarning(string $message): void {
        $this->log("[WARNING] {$message}");
    }

    private function logError(string $message): void {
        $this->log("[ERROR] {$message}");
    }

    private function log(string $message): void
    {
        $logFile = ROOT_D . '/addons/jy_agentmcp/mcp_' . date('Ymd') . '.log';
        $dir = dirname($logFile);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }
        $content = date('Y-m-d H:i:s') . " {$message}\n";
        file_put_contents($logFile, $content, FILE_APPEND);
    }
}
?>